﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using Logger_Handdler;
using System.Data.SqlClient;
namespace XMLManagerLib
{
    public class XMLConnection
    {

        #region Variable
        private static string _xmlPath = string.Empty;
        private static XMLEnum.XMLOperation _CommandType;
        #endregion

        #region properties
        public string XMLFilePath
        {
            get
            {
                return _xmlPath;
            }
            set
            {
                _xmlPath = value;
            }
        }
        public XMLEnum.XMLOperation XMLFileCommandType
        {
            get
            {
                return _CommandType;
            }
            set
            {
                _CommandType = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Defualt Constrcuter
        /// </summary>
        public XMLConnection()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlFilePath"></param>
        public XMLConnection(string xmlFilePath)
        {
            try
            {
                if (string.IsNullOrEmpty(xmlFilePath) && string.IsNullOrEmpty(XMLFilePath))
                {
                    throw new XMLConnectionFailedException("Choose the correct path or the xml file path is empty");
                }
                else
                {
                    if (!File.Exists(xmlFilePath))
                    {
                        throw new XMLConnectionFailedException("XML File Doesn't Exists");
                    }
                    else
                    {
                        if (Path.GetExtension(xmlFilePath).Substring(1) != "xml")
                        {
                            throw new XMLConnectionFailedException("Choose the correct file type like '.xml'");
                        }
                        else
                        {
                            XMLFilePath = _xmlPath;
                            XmlDocument objXmlDocument = new XmlDocument();
                            objXmlDocument.LoadXml(xmlFilePath);
                        }
                    }
                }
            }
            catch (XMLConnectionFailedException XMLConnectionFailedExceptionEx)
            {
                throw XMLConnectionFailedExceptionEx;
            }
            catch (XmlException xmlEx)
            {
                throw xmlEx;
            }
            catch (IOException ioEx)
            {
                throw ioEx;
            }
            catch (Exception ex)
            {
                LogFile.WriteLog(LogFile.WriteLoggerFile.Error, "XMLConnection::XMLConnection(string xmlFilePath)", ex.Message, ex.StackTrace);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public XMLEnum.XmlFileStatus CreateXMLFile(string FileName)
        {
            try
            {
                if (File.Exists(FileName))
                {
                    return XMLEnum.XmlFileStatus.Exists;
                }
                else
                {
                    using (StreamWriter objStreamWriter = new StreamWriter(FileName))
                    {
                        objStreamWriter.Write("<?xml version='1.0' encoding='utf-8'?>");
                    }
                    return XMLEnum.XmlFileStatus.Create;
                }
            }
            catch (XMLConnectionFailedException XMLConnectionFailedExceptionEx)
            {
                return XMLEnum.XmlFileStatus.Unknown;
                throw XMLConnectionFailedExceptionEx;
            }
            catch (XmlException xmlEx)
            {
                return XMLEnum.XmlFileStatus.Unknown;
                throw xmlEx;
            }
            catch (IOException ioEx)
            {
                return XMLEnum.XmlFileStatus.Unknown;
                throw ioEx;
            }
            catch (Exception ex)
            {
                LogFile.WriteLog(LogFile.WriteLoggerFile.Error, "XMLConnection::CreateXMLFile(string FileName)", ex.Message, ex.StackTrace);
                return XMLEnum.XmlFileStatus.Unknown;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="attribute"></param>
        /// <param name="value"></param>
        /// <param name="text"></param>
        public void XMLParameter(string attribute, string value, string text)
        {
            try
            {
                if (XMLFileCommandType == XMLEnum.XMLOperation.Insert)
                {
                    using (StreamWriter objStreamWriter = new StreamWriter(XMLFilePath))
                    {
                        
                        objStreamWriter.Write("");
                    }
                } 
            }
            catch (Exception ex)
            {
                LogFile.WriteLog(LogFile.WriteLoggerFile.Error, "XMLConnection::CreateXMLFile(string FileName)", ex.Message, ex.StackTrace);
            }
        }




        #endregion
    }

    public class XMLConnectionFailedException : System.Exception
    {
        public XMLConnectionFailedException()
        {

        }
        public XMLConnectionFailedException(string message)
            : base(message)
        {

        }
    }

    public class XMLStructure
    {
        private string _attribute = string.Empty;
        private string _value = string.Empty;
        private string _text = string.Empty;

        public string Attribute
        {
            set { _attribute = value; }
            get { return _attribute; }
        }
        public string Value
        {
            set { _value = value; }
            get { return _value; }
        }
        public string Text
        {
            set { _text = value; }
            get { return _text; }
        }

        public XMLStructure(string attribute, string value, string text)
        {
            Attribute = attribute;
            Value = value;
            Text = text;
        }
    }
}
